package cyh.service.array;

import cyh.domain.dto.RoleDto;
import cyh.domain.entity.Role;
import cyh.domain.vo.RoleVo;
import cyh.domain.vo.Result;
import cyh.service.IRoleService;
import cyh.service.IUserService;


public class RoleService extends BaseService<Role,RoleDto, RoleVo> implements IRoleService {

    private IUserService userService ;

    private static RoleService singleton;

    private  RoleService(){
        data= new Role[DATE_SIZE+1];
        temp =new RoleVo[DATE_SIZE];
    }

    public static RoleService getInstance(){
        if(singleton==null)
            singleton=new RoleService();
        return singleton;
    }
    @Override
    RoleVo convertToVo(Role role){
        return new RoleVo(role.getId(),role.name,role.dataScope);
    }

    Role createEntity(RoleDto dto,int id){

        return new Role(id,dto.name,dto.dataScope);
    }

    void updateEntity(RoleDto dto,Role role){
        role.name=dto.name;
        role.dataScope=dto.dataScope;
    }

    @Override
    public  Result delete(int id){
        if(!existById(id))
            return Result.failure("删除失败，角色不存在");
        if(userService==null)
        {
            userService = UserService.getInstance();
        }
        if(userService.roleInUse(id))
            return Result.failure("删除失败，还有用户具有该角色，不能删除");
        data[id]=null;
        return Result.success("删除成功",null);
    }
}